﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class DetailsBySubsidiary : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                byte subId;
                if (byte.TryParse(Request.QueryString["subId"], out subId))
                {
                    LoadVehicles(subId);
                    LoadSupervisors(subId);
                    LoadDrivers(subId);
                }
                else
                {
                    Response.Redirect("Subsidiaries.aspx");
                }
            }
        }

        private void LoadVehicles(byte subId)
        {
            gvVehicles.DataSource = VehicleBLL.GetAllVehicles().Where(v => v.SubsidiaryId == subId).ToList();
            gvVehicles.DataBind();
        }

        private void LoadSupervisors(byte subId)
        {
            var displayList = SupervisorBLL.GetAllSupervisors()
                .Join(StaffBLL.GetAllStaffs().Where(s => s.SubsidiaryId == subId), super => super.StaffId,
                    staff => staff.StaffId, (super, staff) => new
                    {
                        staff.StaffId,
                        staff.FirstName,
                        staff.TelephoneNo,
                        super.SupervisorType,
                        staff.SubsidiaryId
                    }).ToList();

            gvSupervisors.DataSource = displayList;
            gvSupervisors.DataBind();
        }

        private void LoadDrivers(byte subId)
        {
            var displayList = DriverBLL.GetAllDrivers()
                .Join(StaffBLL.GetAllStaffs().Where(s => s.SubsidiaryId == subId), drive => drive.StaffId,
                    staff => staff.StaffId, (drive, staff) => new
                    {
                        staff.StaffId,
                        staff.FirstName,
                        staff.TelephoneNo,
                        drive.LicenseNo,
                        staff.SubsidiaryId
                    }).ToList();

            gvDrivers.DataSource = displayList;
            gvDrivers.DataBind();
        }

        protected void gvVehicles_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvVehicles.PageIndex = e.NewPageIndex;
            var subId = Convert.ToByte(Request.QueryString["subId"]);
            LoadVehicles(subId);
        }

        protected void gvSupervisors_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSupervisors.PageIndex = e.NewPageIndex;
            var subId = Convert.ToByte(Request.QueryString["subId"]);
            LoadSupervisors(subId);
        }

        protected void gvDrivers_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDrivers.PageIndex = e.NewPageIndex;
            var subId = Convert.ToByte(Request.QueryString["subId"]);
            LoadDrivers(subId);
        }

        protected void ibSearchSupervisor_OnClick(object sender, ImageClickEventArgs e)
        {
            int staffId;
            var subId = Convert.ToByte(Request.QueryString["subId"]);

            if (!int.TryParse(tbSearchSupervisor.Text, out staffId))
            {
                LoadSupervisors(subId);
            }
            else
            {
                var staff = StaffBLL.GetStaffById(staffId);

                if (staff == null || staff.SubsidiaryId != subId)
                {
                    gvSupervisors.DataSource = null;
                }
                else
                {
                    var supervisor = new Supervisor(staff)
                    {
                        SupervisorType = SupervisorBLL.GetSupervisorTypeById(staffId)
                    };

                    gvSupervisors.DataSource = supervisor.SupervisorType == null
                        ? null
                        : new List<Supervisor> {supervisor};
                }

                gvSupervisors.DataBind();
            }
        }

        protected void ibVehicleSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            var keyword = tbVehicleSearch.Text.Trim();
            var subId = Convert.ToByte(Request.QueryString["subId"]);

            if (string.IsNullOrEmpty(keyword))
            {
                LoadVehicles(subId);
            }
            else
            {
                var vehicle = VehicleBLL.GetVehiclesByRegNo(keyword);

                gvVehicles.DataSource = vehicle != null && vehicle.SubsidiaryId == subId
                    ? new List<Vehicle> {vehicle}
                    : null;
                gvVehicles.DataBind();
            }
        }

        protected void ibDriverSearch_OnClick(object sender, ImageClickEventArgs e)
        {
            int staffId;
            var subId = Convert.ToByte(Request.QueryString["subId"]);

            if (!int.TryParse(tbDriverSearch.Text, out staffId))
            {
                LoadDrivers(subId);
            }
            else
            {
                var staff = StaffBLL.GetStaffById(staffId);

                if (staff == null)
                {
                    gvDrivers.DataSource = null;
                }
                else
                {
                    var driver = new Driver(staff)
                    {
                        LicenseNo = DriverBLL.GetLicenceNo(staffId)
                    };

                    gvDrivers.DataSource = driver.LicenseNo != null && driver.SubsidiaryId == subId
                        ? new List<Driver> {driver}
                        : null;
                }

                gvDrivers.DataBind();
            }
        }

        protected void ibDeleteSupervisor_OnClick(object sender, ImageClickEventArgs e)
        {
            var staffId = Convert.ToInt32(Request.QueryString["StaffId"]);
            var subId = Convert.ToByte(Request.QueryString["subId"]);
            StaffBLL.RemoveStaff(staffId);
            LoadSupervisors(subId);
        }

        protected void ibDeleteDriver_OnClick(object sender, ImageClickEventArgs e)
        {
            var staffId = Convert.ToInt32(Request.QueryString["StaffId"]);
            var subId = Convert.ToByte(Request.QueryString["subId"]);
            StaffBLL.RemoveStaff(staffId);
            LoadDrivers(subId);
        }

        protected void ibDeleteVehicle_OnClick(object sender, ImageClickEventArgs e)
        {
            var regNo = Request.QueryString["RegNo"];
            var subId = Convert.ToByte(Request.QueryString["subId"]);
            VehicleBLL.RemoveVehicle(regNo);
            LoadVehicles(subId);
        }
    }
}